package com.magic.uschat;

import org.greenrobot.eventbus.EventBus;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.magic.uschat.event.FinishEvent;
import com.magic.uschat.model.BaseModel;
import com.magic.uschat.model.UserModel;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


@ContentView(R.layout.activity_register)
public class RegisterActivity extends ParentWithNaviActivity {

	@ViewInject(R.id.et_username)
	private EditText et_username;
	@ViewInject(R.id.et_passwd)
	private EditText et_passwd;
	@ViewInject(R.id.et_passwd_again)
	private EditText et_passwd_again;
	
	@ViewInject(R.id.btn_register)
	private Button btn_register;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initNaviView();
	}

	@Override
	protected String title() {
		return getString(R.string.register);
	}
	
	@OnClick(R.id.btn_register)
	public void onRegisterClick(View v){
		UserModel.instance().register(et_username.getText().toString(), et_passwd.getText().toString(), et_passwd_again.getText().toString(), new LogInListener<Object>() {
			@Override
			public void done(Object obj, BmobException e) {
				if (e==null) {
					EventBus.getDefault().post(new FinishEvent());
					startActivity(MainActivity.class, null, true);
				}else {
					if (e.getErrorCode()==BaseModel.CODE_NOT_EQUAL) {
						et_passwd_again.setText("");
					}
					toast(e.getMessage()+"("+e.getErrorCode()+")");
				}
			}
		});
	}
}
